#!/bin/bash

echo "Testing MiniBug application..."

# 启动应用程序
echo "Starting application..."
./minibug-dev &
APP_PID=$!

# 等待应用程序启动
sleep 3

# 测试登录页面
echo "Testing login page..."
curl -s http://localhost:8080/login | grep -q "MiniBug 登录" && echo "✓ Login page accessible" || echo "✗ Login page failed"

# 测试API登录
echo "Testing API login..."
LOGIN_RESPONSE=$(curl -s -X POST http://localhost:8080/api/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin123"}')

if echo "$LOGIN_RESPONSE" | grep -q "登录成功"; then
    echo "✓ API login successful"
else
    echo "✗ API login failed"
    echo "Response: $LOGIN_RESPONSE"
fi

# 测试获取项目列表
echo "Testing projects API..."
PROJECTS_RESPONSE=$(curl -s -X GET http://localhost:8080/api/projects \
  -H "Cookie: session_id=admin")

if echo "$PROJECTS_RESPONSE" | grep -q "Web应用"; then
    echo "✓ Projects API working"
else
    echo "✗ Projects API failed"
fi

# 测试获取Bug列表
echo "Testing bugs API..."
BUGS_RESPONSE=$(curl -s -X GET http://localhost:8080/api/bugs \
  -H "Cookie: session_id=admin")

if echo "$BUGS_RESPONSE" | grep -q "bugs"; then
    echo "✓ Bugs API working"
else
    echo "✗ Bugs API failed"
fi

# 停止应用程序
echo "Stopping application..."
kill $APP_PID 2>/dev/null

echo "Test completed!"
